package com.yjx.controller;

import com.yjx.client.UserClient;
import com.yjx.conent.ResultCode;
import com.yjx.dto.ResourceDto;
import com.yjx.dto.RoleDto;
import com.yjx.entity.Role;
import com.yjx.entity.User;
import com.yjx.util.Result;
import com.yjx.util.ResultUtil;
import com.yjx.vo.PageVo;
import com.yjx.vo.ParentResourceVo;
import com.yjx.vo.RoleVo;
import com.yjx.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-04-15 20:48:14
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserClient userClient;

    @PostMapping("/login")
    public Result<UserVo> login(User user) {
        UserVo userVo = userClient.login(user);
        if (null == userVo) {
            return ResultUtil.errorResult(ResultCode.ERROR_CODE, ResultCode.ERROR_MSG);
        }
        return ResultUtil.successDataResult(userVo);
    }

    @PostMapping("/getOperateToken")
    public Result getOperateToken(){
        return userClient.getOperateToken();
    }

    @PostMapping("/checkRoleCode")
    public Result checkRoleCode(String roleCode){
        return userClient.checkRoleCode(roleCode);
    }

    @PostMapping("/createRole")
    public Result saveRole(Role role, HttpServletRequest request){
        String token = request.getHeader("token");
        String operateToken = request.getHeader("operateToken");
        return userClient.saveRole(role,token,operateToken);
    }

    @PostMapping("/deleteRoleById")
    public Result deleteRoleById(Integer id){
        return userClient.deleteRoleById(id);
    }

    @PostMapping("/checkUserCode")
    public Result checkUserCode(@RequestParam("userCode") String userCode){
        return userClient.checkUserCode(userCode);
    }

    @PostMapping("/createUser")
    public Result createUser(User user,HttpServletRequest request){
        String token = request.getHeader("token");
        return userClient.createUser(user,token);
    }

    @PostMapping("/queryAllResource")
    public Result<List<ParentResourceVo>> queryAllResource(){
        return userClient.queryAllResource();
    }
    @PostMapping("/saveResource")
    public Result saveResource(ResourceDto resourceDto,HttpServletRequest request){
        String token = request.getHeader("token");
        return userClient.saveResource(resourceDto,token);
    }

    @PostMapping("queryAllRole")
    public Result<List<RoleVo>> queryAllRole(){
        return userClient.queryAllRole();
    }
    @PostMapping("/createUserRole")
    public Integer createUserRole(RoleDto roleDto){
        return userClient.createUserRole(roleDto);
    }

    @PostMapping("/queryRoleByRoleName")
    public Result<PageVo<RoleVo>> queryRoleByRoleName(Integer pageNum,Integer pageSize,String roleName){
        return userClient.queryRoleByRoleName(pageNum,pageSize,roleName);
    }

    @PostMapping("/queryUserByUserName")
    public Result<PageVo<UserVo>> queryUserByUserName(Integer pageNum,Integer pageSize,String userName){
        return userClient.queryUserByUserName(pageNum, pageSize, userName);
    }



}
